package manage.actions.group;

import java.util.List;

import manage.actions.ManageAction;
import manage.commons.annontations.AccessControl;
import manage.commons.annontations.AccessVerify;
import manage.commons.annontations.RequiredLogin;
import manage.commons.utils.CommonUtil;
import manage.commons.utils.InputStreamUtil;
import manage.commons.utils.Pager;
import manage.modules.dept.service.DeptService;
import manage.modules.group.model.Group;
import manage.modules.group.service.GroupService;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.util.CollectionUtils;

@RequiredLogin
@AccessVerify
public class GroupAction extends ManageAction {

	private static final long serialVersionUID = 4446571107104957840L;
	private GroupService groupService;
	private DeptService deptService;
	private Pager<Group> pager = new Pager<Group>(100);
	private Group group;
	private String[] deptIds;

	@Override
	@AccessControl
	@Action("group-input")
	public String input() {
		return SUCCESS;
	}

	@Override
	@AccessVerify(-1)
	@Action(value = "group-addorupdate", results = { @Result(name = SUCCESS, location = "/group/group-form.jsp") })
	public String forwardAddOrUpdatePage() {
		if (isAddOperater()) {
			action = "/group/group-add.do";
			getRequest().setAttribute("btnKeyWord", "添加组别");
		} else if (isUpdateOperater()) {
			getRequest().setAttribute("btnKeyWord", "更新组别");
			getRequest().setAttribute("curDepts",
					deptService.listDeptByGroupId(group.getGroupId()));
			group = groupService.select(group.getGroupId());
			action = "/group/group-edit.do";
		}
		getRequest().setAttribute("allDept", deptService.listAll());
		return SUCCESS;
	}

	@AccessControl
	@Action(value = "group-query", results = { @Result(name = SUCCESS, location = "/group/group-list.jsp") })
	public String query() throws Exception {
		if (group != null) {
			groupService.query(group, pager);
			pager.pagerUtils(pager, getRequest());
		}
		return SUCCESS;
	}

	@AccessVerify(-1)
	@Action(value = "group-select", results = { @Result(name = SUCCESS, type = "json", params = {
			"root", "group" }) })
	public String select() {
		if (null != group && null != group.getGroupId()) {
			boolean exists = groupService.exists(group.getGroupId());
			if (exists) {
				group = groupService.select(group.getGroupId());
			}
		}
		return SUCCESS;
	}

	@Action(value = "group-add", results = { @Result(name = SUCCESS, type = "stream", params = {
			"contentType", "text/plain; charset=utf-8", "inputName",
			"inputStream" }) })
	public String add() throws Exception {
		if (null != group && null != group.getGroupId()
				&& group.getGroupName() != null && null != group.getDisabled()) {
			boolean exists = groupService.exists(group.getGroupId());
			if (!exists) {
				int count = groupService.insert(group);
				if (count == 1) {
					if (ArrayUtils.isNotEmpty(deptIds)) {
						deptService.insertGroupOfDepts(group.getGroupId(),
								CommonUtil.evalStringArrayToLongList(deptIds));
					}
					inputStream = InputStreamUtil.getSuccessInputStream();
				}
			} else {
				inputStream = InputStreamUtil.getExistsInputStream();
			}
		} else {
			inputStream = InputStreamUtil.getFailedInputStream();
		}
		return SUCCESS;
	}

	@Action(value = "group-delete", results = { @Result(name = SUCCESS, type = "stream", params = {
			"contentType", "text/plain; charset=utf-8", "inputName",
			"inputStream" }) })
	public String delete() throws Exception {
		if (null != group && null != group.getGroupId()) {
			boolean exists = groupService.exists(group.getGroupId());
			if (exists) {
				if (!groupService.workerVested(group.getGroupId())) {
					int delNum = groupService.delete(group.getGroupId());
					if (delNum == 1) {
						inputStream = InputStreamUtil.getSuccessInputStream();
					}
				} else {
					inputStream = InputStreamUtil.toInputStream("workervested");
				}
			} else {
				inputStream = InputStreamUtil.getNotExistsInputStream();
			}
		} else {
			inputStream = InputStreamUtil.getFailedInputStream();
		}
		return SUCCESS;
	}

	@Action(value = "group-edit", results = { @Result(name = SUCCESS, type = "stream", params = {
			"contentType", "text/plain; charset=utf-8", "inputName",
			"inputStream" }) })
	public String edit() throws Exception {
		if (null != group && null != group.getGroupId()
				&& group.getGroupName() != null && null != group.getDisabled()) {
			boolean exists = groupService.exists(group.getGroupId());
			if (exists) {
				groupService.update(group);
				List<Long> dpIds = groupService.listDeptIdByGroupId(group
						.getGroupId());
				Long[] dpts = CommonUtil.evalLongArray(deptIds);
				List<Long> addValues = CommonUtil.operatorAddValue(dpts, dpIds);
				List<Long> reduceValues = CommonUtil.operatorReduceValue(dpIds,
						dpts);
				if (!CollectionUtils.isEmpty(addValues) && addValues.size() > 0) {
					deptService.insertGroupOfDepts(group.getGroupId(),
							addValues);
				}
				if (!CollectionUtils.isEmpty(reduceValues)
						&& reduceValues.size() > 0) {
					deptService.deleteGroupOfDepts(group.getGroupId(),
							reduceValues);
				}
				inputStream = InputStreamUtil.getSuccessInputStream();
			} else {
				inputStream = InputStreamUtil.getNotExistsInputStream();
			}
		} else {
			inputStream = InputStreamUtil.getFailedInputStream();
		}
		return SUCCESS;
	}

	public Pager<Group> getPager() {
		return pager;
	}

	public void setPager(Pager<Group> pager) {
		this.pager = pager;
	}

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}

	public void setDeptIds(String[] deptIds) {
		this.deptIds = deptIds;
	}

}
